﻿var addcontactGrid = addcontactGrid || {};

addcontactGrid = function () {
    var gridInEditMode = false;
    var addcontactDropDownChanged = false;
    var addcontactDataSource = new kendo.data.DataSource({
        transport: {
            read: {
                url: "/AddContact/GetAddContacts",
                type: "get",
                dataType: "json"
            },
            create: {

                url: "/AddContact/CreateAddContact",
                type: 'POST',
                dataType: "json",
                contentType: "application/JSON"

            },
            update: {
                url: "/AddContact/UpdateAddContact",
                type: 'POST',
                dataType: "json",
                contentType: "application/JSON"
            },
            destroy: {
                url: "/ScratchPad/DeleteAddContact",
                type: "POST",
                dataType: "json",
                contentType: "application/JSON"
            },
            parameterMap: function (options) {
                return kendo.stringify(options); // kendo.stringify serializes to JSON string
            }
        },

        pageSize: 10,
        schema: {
            model: {
                id: "id",
                fields: {
                    id: {
                        editable: false, nullable: true,
                        type: "int"
                    },
                    name: {
                        type: "string",
                        validation: { required: true }
                    },
                    email: {

                        type: "string",
                        validation: { required: true }
                    },

                    companyid: {
                        editable: true,
                        type: "int"
                    },
                    company: {
                        type: "string",
                        validation: { required: true }
                    },
                    businessPhone: {
                        editable: true,
                        type: "string"
                    },
                    mobilePhone: {
                        type: "string",
                        validation: { required: true }
                    },
                    contactTypeName: {
                        type: "string",
                        validation: { required: true }
                    },
                    jobTitle:
                        {
                            type: "string",
                            editabl: true,
                        },
                    department: {
                        type: "string",
                        validation: { required: true }
                    },
                    startDate: {
                        type: "date"
                    },
                    office: {
                        type: "string",
                        validation: { required: true }
                    },

                    notes: {
                        type: "string"
                        // validation: { required: true}
                    },

                    error: function (a) {
                        $('#addcontactGrid').data("kendoGrid").cancelChanges();
                    },
                }
            }
        }

    });

    var company = new kendo.data.DataSource({
        transport: {
            read: {
                url: "/ScratchPad/GetJustCompanyName/",
                type: "get",
                contentType: "application/json",
                dataType: "json"
            }
        }
    });
    var contactDetails = new kendo.data.DataSource({
        transport: {
            read: {
                url: "/ScratchPad/GetContactDetail/",
                type: "get",
                contentType: "application/json",
                dataType: "json"
            }
        },
        requestEnd: function (a, b) {

        }

    });

    var contantType = new kendo.data.DataSource({
        transport: {
            read: {
                url: "/AddContact/GetContactTypes/",
                type: "get",
                contentType: "application/json",
                dataType: "json"
            }
        }
    });
    var jobTitle = new kendo.data.DataSource({
        transport: {
            read: {
                url: "/AddContact/GetJobTitles/",
                type: "get",
                contentType: "application/json",
                dataType: "json"
            }
        }
    });

    var department = new kendo.data.DataSource({
        transport: {
            read: {
                url: "/AddContact/GetDepartments/",
                type: "get",
                contentType: "application/json",
                dataType: "json"
            }
        }
    });

    return {
        addcontactDataSource: addcontactDataSource,
        company: company,
        contactDetails: contactDetails,
        contantType: contantType,
        jobTitle: jobTitle,
        department: department,
        addcontactDropDownChanged: addcontactDropDownChanged,
        gridInEditMode: gridInEditMode


    }

}();

$(function () {

    function dateTimeEditor(container, options) {
        $('<input data-text-field="' + options.field + '" data-value-field="' + options.field + '" data-bind="value:' + options.field + '" data-format="' + options.format + '"/>')
        .appendTo(container)
        .kendoDateTimePicker({});
    }

    $("#addcontactGrid").kendoGrid({
        dataSource: addcontactGrid.addcontactDataSource,
        scrollable: true,
        height: "250px",
        toolbar: ["create"], //, "save", "cancel"],
        pageable: true,
        height: 500,
        columns: [
     // { field: "LogsDate", title: "LogsDate*", filterable: false },
      { field: "name", title: "FullName", editor: contactAutoCompleteEditor, filterable: false },
      { field: "email", title: "E-Mail", width: "8em" },
      { field: "company", title: "Company", width: "8em", editor: companyDropDownEditor },
      { field: "businessPhone", title: "Phone(Office)", width: "8em" },
      { field: "mobilePhone", title: "Phone(Cell)", width: "8em" },
      { field: "contactTypeName", title: "Contact Type", editor: contactTypeDropDownEditor, width: "8em" },
      { field: "jobTitle", title: "Job Title", editor: jobTitleDropDownEditor, width: "8em" },
      { field: "department", title: "Department", editor: departmentDropDownEditor, width: "8em" },
         { field: "office", title: "Office", width: "8em" },
          { field: "notes", title: "Note", width: "8em" },
    {
        field: "startDate", title: "Start Date", filterable: false, format: "{0:MM/dd/yyyy HH:mm tt}",
        editor: dateTimeEditor,
        filterable: {
            ui: "datetimepicker"
        }
    },
      {
          title: "&nbsp;",
          width: "12em",
         // command: ["edit", "destroy"]
          command: [

              "edit",
              {
                  name: "Delete",


                  click: function (e) {
                      debugger;
                      var tr = $(e.target).closest("tr"); //get the row for deletion
                      var dataItem = this.dataItem(tr); //get the row data so it can be referred later
                      //see what is coming in dataItem
                      //if (dataItem.QuestionAnswers.length > 0) {
                      //    $("#delValidation").addClass('in')
                      //}
                      //else {

                          deleteTopicConfirmation(e, this);
                     // }
                      return false;
                  }
              }
          ]
      }
        ],
        filterable: {
            extra: false,
            operators: {
                string: {
                    eq: "Is equal to",
                    neq: "Is not equal to"
                }
            }
        },

        editable: "popup",
        sortable: {
            mode: "single",
            allowUnsort: false
        },
        selectable: true,
        edit: function () {
            addcontactGrid.gridInEditMode = true;
        },
        cancel: function () {
            addcontactGrid.gridInEditMode = false;
        }
    });

    addcontactGrid.addcontactDataSource.bind("requestEnd", showSuccessMessage);


    function showSuccessMessage(e) {
        if (e.type === "update" || e.type === "create") {
            addcontactGrid.gridInEditMode = false;
            $("#saveConfirm").addClass('in');
            e.sender.read();
        }
    }


    $(".close").click(function () {
        $(this).parent().removeClass('in');
    });


    function deleteTopicConfirmation(e, widget) {
        var grid = $('#scratchGrid').data("kendoGrid");
        deleteConfirmation(e, widget, grid);
    }

    function deleteConfirmation(e, widget, grid) {

        var currentRow = $(e.target).closest("tr"); //get the row for deletion

        var currentRowData = widget.dataItem(currentRow); //get the row data so it can be  referred later

        var kendoWindow = $("<div />").kendoWindow({

            title: "Confirmation",
            resizable: true,
            modal: true,
            width: "400px"
        });

        kendoWindow.data("kendoWindow")

            .content($("#delete-confirmation").html())

            .center().open();

        kendoWindow

            .find(".delete-confirm,.delete-cancel")
            .click(function () {
                if ($(this).hasClass("delete-confirm")) {

                    grid.dataSource.remove(currentRowData);

                    grid.dataSource.sync();

                }

                $(this).closest("[data-role=window]").kendoWindow("close");

            });

    }


    function companyDropDownEditor(container, options) {

        $('<input data-text-field="cpName" data-value-field="cpName" data-bind="value:' + options.field + '"/>').appendTo(container).kendoDropDownList({
            autoBind: false,
            dataSource: addcontactGrid.company

        });
    }
    function contactTypeDropDownEditor(container, options) {

        $('<input data-text-field="description" data-value-field="description"  data-bind="value:' + options.field + '"/>').appendTo(container).kendoDropDownList({
            autoBind: false,
            dataSource: addcontactGrid.contantType
        });
    }
    function jobTitleDropDownEditor(container, options) {

        $('<input data-text-field="code" data-value-field="code"  data-bind="value:' + options.field + '"/>').appendTo(container).kendoDropDownList({
            autoBind: false,
            dataSource: addcontactGrid.jobTitle

        });
    }
    function departmentDropDownEditor(container, options) {

        $('<input data-text-field="departmentCode" data-value-field="departmentCode"  data-bind="value:' + options.field + '"/>').appendTo(container).kendoDropDownList({
            autoBind: false,
            dataSource:
                addcontactGrid.department

        });
    }
    function contactAutoCompleteEditor(container, options) {
        $('<input data-text-field="name"  data-value-field="Name"  data-bind="value:' + options.field + '"/>')
        .appendTo(container)
        .kendoAutoComplete({
            autoBind: true,
            suggest: true,
            placeHolder: "enter caller name here",
            filter: "contains",
            //   index: 1,
            minLength: 1,
            dataSource: addcontactGrid.contactDetails
        });
    }


});


